const express = require("express");
const db = require("./model/db.js");

const app = express()

//添加用户
app.get("/api/add", (req, res) => {
	db.add({ collection: "users", data: [{ realname: "张三", phone: "18301682292" }] })
		.then(result => {
			res.json(result)
		})
		.catch(err => {
			res.json(err)
		})
})

//获取用户
app.get("/api/list", async (req, res) => {
	let result = await db.get({ collection: "users" }).catch(err => {
		console.error(err)
	})
	res.json(result)

})

//更新用户
app.get("/api/update", async (req, res) => {
	let result = await db.update({
		collection: "users",
		where: { realname: "张三" },
		data: { phone: "1831111111" }
	}).catch(err => {
		console.error(err)
	})


	res.json(result)

})


//更新用户
app.get("/api/updatebyid", async (req, res) => {
	let result = await db.updateById({
		collection: "users",
		id: "28ee4e3e6096295d16de50580db6875e",
		data: { realname: "李思奎" }
	}).catch(err => {
		console.error(err)
	})
	res.json(result)

})

//更新用户
app.get("/api/remove", async (req, res) => {
	let result = await db.removeById({
		collection: "users",
		id: "17453ede609629a70832573b4fd252f4"
	}).catch(err => {
		console.error(err)
	})
	res.json(result)

})



app.listen(3000, () => {
	console.log("--3001--")
})